package com.fgs.common;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.fgs.common.utils.LogUtil;

import java.lang.ref.WeakReference;

/**
 * @author fgs
 * @date 2020/6/16
 * @description java.lang.IllegalArgumentException: View=PhoneWindow$DecorView{...} not attached to window manager
 * 针对此问题本文主要是抛出解决方案，定义一个基类BaseDialog，其他Dialog继承此基类
 **/
//public abstract class CommonDialog extends Dialog implements LifecycleObserver {
public abstract class CommonDialog extends Dialog {

//    private Context mContext;

    public CommonDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public CommonDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    private void init(Context context) {
//        mContext = context;
//        if (context instanceof CommonActivity) {
//            ((CommonActivity) context).getLifecycle().addObserver(this);
//        }
    }

//    @Override
//    public void dismiss() {
//        if (mContext instanceof CommonActivity) {
//            CommonActivity activity = (CommonActivity) mContext;
//            if (!activity.isFinishing()) {
//                super.dismiss();
//            }
//        }
//    }

//    @Override
//    public void show() {
//        if (mContext instanceof CommonActivity) {
//            CommonActivity activity = (CommonActivity) mContext;
//            if (!activity.isFinishing() && !activity.isDestroyed()) {
//                super.show();
//            }
//        }
//    }


//    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
//    public void onDestroy() {
//        if (isShowing()) {
//            dismiss();
//        }
//        mContext = null;
//        LogUtil.print("CommonDialog onDestroy");
//    }
}
